package kd.bos.kdec.pincar;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.bill.MobileBillShowParameter;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.filter.FilterParameter;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.list.events.ListRowClickEvent;
import kd.bos.list.events.ListRowClickListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;

/**
 * i拼车行程单列表
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobBillList extends AbstractFormPlugin implements ListRowClickListener {

	private String[] buttons = new String[] { "pincarclick", "generateorder", "dingbill", "myclick", "quanclick" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
		BillList billList = this.getControl("billlistap");
		billList.addListRowClickListener(this);
	}

	@Override
	public void listRowClick(ListRowClickEvent evt) {// 列表卡片点击事件
		ListRowClickListener.super.listRowClick(evt);
		// 详情跳转
		BillList billList = this.getControl("billlistap");
		ListSelectedRow listSelectedRow = billList.getCurrentSelectedRowInfo();
		Long pk = (Long) listSelectedRow.getPrimaryKeyValue();
		MobileBillShowParameter showParameter = new MobileBillShowParameter();
		showParameter.setFormId("kdec_pincarbillmob");
		showParameter.setPkId(pk);
		showParameter.getOpenStyle().setShowType(ShowType.Floating);
		this.getView().showForm(showParameter);

	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		Map<String, Object> formId = new HashMap<>();
		if (StringUtils.equals("pincarclick", key)) {// 首页跳转
			formId.put("formId", "kdec_pincar");
			this.showForm(formId);
		} else if (StringUtils.equals("dingbill", key)) {// 订单点击刷新
			BillList billList = this.getControl("billlistap");
			billList.refreshData();
		} else if (StringUtils.equals("myclick", key)) {// 我的 跳转
			formId.put("formId", "kdec_pincar_my");
			this.showForm(formId);
		} else if (StringUtils.equals("quanclick", key)) {// 圈子
			formId.put("formId", "kdec_pincar_my");
			this.showForm(formId);
		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		BillList billList = this.getControl("billlistap");
		int count = billList.getListModel().getBillDataCount();
		if (count == 0) {//列表无数据展示无数据页面
			this.getView().setVisible(true, "flexpanelap");
			this.getView().setVisible(false, "billlistap");
		}
		billList.refreshData();
	}

	@Override
	public void beforeBindData(EventObject e) {
		super.beforeBindData(e);
		// 根据登录用户Id隔离数据
		long userId = Long.parseLong(RequestContext.get().getUserId());
		// 加载单据列表添加筛选条件
		BillList billList = this.getControl("billlistap");
		FilterParameter filterParameter = new FilterParameter();
		QFilter filter = new QFilter("creator", QCP.equals, userId);
		filterParameter.setFilter(filter);
		billList.setFilterParameter(filterParameter);
	}

	/**
	 * 页面跳转通用方法
	 * 
	 * @param formId
	 *            页面标识
	 */
	private void showForm(Map<String, Object> formId) {
		FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
		showParameter.getOpenStyle().setShowType(ShowType.Floating);
		this.getView().showForm(showParameter);
	}
}
